layui.use(['jquery','layer','table','form'],function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;

    var userSearch = window.location.search;
    var userSplit = userSearch.split("=");
    var userId = userSplit[userSplit.length-1];
   // var couponName="";
    select("");
    function select(couponName){
        table.render({
            elem:"#root",
            url:"/Coupons/selectGiveCoupons?couponName="+couponName,
            limit: 5,
            page: {
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: "首页",
                last: "尾页",
            },
            size: 'lg',
            cols:[[
                {type:"checkbox",align:"center"},
                {type:"numbers",title:"序号",align:"center"},
                {field:"couponName",title:"优惠券名称",align:"center"},
                {field:"couponPrice",title:"优惠券面额",align:"center",templet:function (data) {
                        return "￥"+data.couponPrice;
                    }},
                {field:"type",title:"优惠券类型",align:"center",templet:function (data) {
                        if(data.type=="2"){
                            return "商品";
                        }else{
                            return "视频";
                        }
                    }},
                {title:"有效期",align:"center",width:200,templet:function (data) {
                        return "开始:"+data.startDate+"结束:"+data.endDate
                    }}
            ]]
        })
    };
    //查询
    $("#chaxun").click(function () {
        var couponName = $("#couponName").val();
        select(couponName);
    });
    //重置
    $("#chongzhi").click(function () {
        select("");
    });

        $("#queding").click(function () {
            var checkStatus = table.checkStatus("root").data;
               if(checkStatus.length>0){
                   checkStatus.forEach(function (data) {
                       var getDate = new Date();
                       getDate = getTime(getDate);
                       var userId = $("#userId").val();
                       var json = {
                           userId:userId,
                           couponId:data.couponId,
                           getDate:getDate,
                           remarks:2
                       };
                       //console.log(json);
                       $.ajax({
                           type:'post',
                           javaType:"json",
                           data:json,
                           url:"/Coupons/addBeforeCoupons",
                           success:function (data) {
                               if(data){
                                   layer.msg("赠送成功");
                               }else{
                                   layer.msg("赠送失败");
                               }
                               var index  = parent.layer.getFrameIndex(window.name);
                               parent.layer.close(index);
                           }
                       })
                   });
               }else{
                   layer.msg("必须选择一个");
               }
        });
    //取消
    $("#quxiao").click(function () {
        var index  = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };

    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };

});